/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trunk.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author usuario
 */
public class TelaAnexos extends JFrame{
    
    private final JPanel topPanel;
    private final JScrollPane scrollpane;
    private final JPanel botPanel;
    private final JButton downloadButton;
    private final JButton cancelButton;
    private String arquivo;
    private JList list;
    private DefaultListModel listModel;
    private final JButton deleteButton;
    
    public TelaAnexos(final ArrayList<String> anexos) {  
       super("Download de Anexos"); 
       setSize(300,217);
       setBackground( Color.gray );
       setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       
       topPanel = new JPanel();
       topPanel.setLayout( new BorderLayout() );
       botPanel = new JPanel();   
       
       listModel = new DefaultListModel();
       
       for (int i=0; i<anexos.size();i++){
         listModel.addElement(anexos.get(i));
       
       }
       
        list = new JList(listModel);
        
        list.addListSelectionListener( new ListSelectionListener() {  
           public void valueChanged(ListSelectionEvent e) {  
              Object object = list.getSelectedValue();  
                arquivo = (String) object;
           }  
        });
        scrollpane = new JScrollPane(list);
        
        topPanel.add( scrollpane, BorderLayout.CENTER );
//Botões    
         downloadButton = new JButton("Download");
         
         downloadButton.addActionListener(new ActionListener() {           
            public void actionPerformed(ActionEvent ae) {
                
                if(arquivo==null){
                    JOptionPane.showMessageDialog(list, "Selecione um arquivo primeiro");
                }
                else{
                    System.out.println(arquivo);
                   
                }
            }            
        });    
       
        
        cancelButton = new JButton("Cancelar");
        
        cancelButton.addActionListener(new ActionListener() {           
            public void actionPerformed(ActionEvent ae) {
                dispose();                
            }            
        });
       
        
        deleteButton = new JButton("Delete");
        
        deleteButton.addActionListener(new ActionListener() {           
            public void actionPerformed(ActionEvent ae) {
                if(arquivo==null){
                    JOptionPane.showMessageDialog(list, "Selecione um arquivo primeiro");
                }
                else{
                    anexos.remove(arquivo);
                    listModel.removeElement(arquivo);
                    repaint();
                    System.out.println(anexos);
                }
            }            
        });
 
//Adiciona os Componentes       
       botPanel.add(downloadButton);
       botPanel.add(deleteButton);
       botPanel.add(cancelButton);
       getContentPane().add( topPanel, BorderLayout.NORTH );
       getContentPane().add( botPanel, BorderLayout.SOUTH);
}  
     
}
